<template>
  <div class="app-container">
    <el-form size="small">
      <el-form-item>
        <el-button type="primary" @click="add">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="loading"
      :data="list"
      style="width: 100%;"
    >
      <el-table-column label="序号" type="index" >
        <template slot-scope="scope">
          <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
        </template>
      </el-table-column>
      <el-table-column label="Banner名称"  prop="title" />
      <el-table-column label="状态" >
        <template slot-scope="scope">
          <span v-show="scope.row.status == '1'">激活</span>
          <span v-show="scope.row.status == '0'">未激活</span>
        </template>
      </el-table-column>
      <el-table-column label="说明"  prop="remark" />
      <el-table-column label="操作" >
        <template slot-scope="scope">
          <el-button type="text" style="color:#386BD7" @click="edit(scope.row)">编辑</el-button>
          <el-button type="text" style="color:#FF5555" @click="del(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
    <!-- 添加 修改终端 -->
    <el-dialog :title="title" :visible.sync="open" width="30%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="名称" prop="title">
          <el-input v-model="form.title" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" clearable style="width:100%">
            <el-option
              v-for="item in statusList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="说明" prop="remark">
          <el-input type="textarea" v-model="form.remark" placeholder="请输入" resize="none" :rows="4"></el-input>
        </el-form-item>
        <el-form-item label="上传图片" prop="remark">
          <div v-if="fileList.length > 0" style="display:flex;align-items:center">
            <span style="color:#386BD7">{{fileList[0].name}}</span>
            <span style="color:#386BD7;font-size:20px;cursor:pointer;margin-left:5px" @click="delFile">×</span>
            <el-button style="margin-left:5px" type="primary" size="small" @click="preview">浏览</el-button>
          </div>
          <el-upload
          v-else
          class="upload"
          accept=".png,.jpg"
          :file-list="fileList"
          :on-change="handleChange"
          :show-file-list="false"
          >
            <el-button style="background-color:#67C23A;border:1px solid #67C23A;color:#fff" size="small">上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="open = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog
    title="提示"
    :visible.sync="delVisible"
    width="30%">
      <span>是否删除？</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="delVisible = false">取 消</el-button>
        <el-button type="primary" @click="delConfirm">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog title="浏览" :visible.sync="previewVisible" width="50%" append-to-body>
      <img style="width:100%" :src="previewImg" alt="">
    </el-dialog>
  </div>
</template>

<script>
import { getBannerList,addBanner,updateBanner,delBanner } from "@/api/common";

export default {
  name: "Online",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },
      statusList:[
        {value:'1',label:'激活'},
        {value:'0',label:'未激活'},
      ],
      form:{},
      rules: {
        title: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
        status:[
          { required: true, message: "状态不能为空", trigger: "blur" }
        ],
      },
      title:'',
      open:false,
      delVisible:false,
      previewVisible:false,
      fileList:[],
      previewImg:''
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      getBannerList(this.queryParams).then(response => {
        this.list = response.rows
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams =  {
        pageNum: 1,
        pageSize: 10
      }
      this.handleQuery();
    },
    handleSelectionChange(val){
      this.selection = val
    },
    add(){
      this.form = {}
      this.open = true;
      this.title = "新增Banner";
      this.fileList = []
    },
    edit(row){
      this.form = row
      this.open = true;
      this.title = "编辑Banner";
      this.form.name = this.form.fileName
      this.fileList = [row]
    },
    del(row){
      this.form = row
      this.delVisible = true
    },
    delConfirm(){
      this.delVisible = false
      delBanner(this.form.id).then(res=>{
        if(res.code == 200){
          this.$message.success('删除成功')
          this.getList()
        }
      })
    },
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          let form = new FormData()
          form.append('title',this.form.title)
          form.append('status',this.form.status)
          form.append('remark',this.form.remark)
          console.log(this.fileList);
          if(this.fileList.length > 0 && this.fileList[0].uid) {
            form.append('file',this.fileList[0])
          }
          if (this.form.id != undefined) {
            // updateBanner(form).then(response => {
              addBanner(form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBanner(form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    handleChange(file){
      console.log(file);
      this.fileList = [file.raw]
    },
    delFile(){
      this.fileList = []
    },
    preview(){
      this.previewVisible = true
      this.previewImg = this.fileList[0].url
    }
  }
};
</script>

<style lang="scss" scoped>
.warnStatus{
  display: flex;
  justify-content: center;
}
.circle{
  width: 8px;
  height: 8px;
  border-radius: 50%;
}
.terminalContent{
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 40px;
  .terminalItem{
    width: 50%;
  }
}
.dealContent{
  display: flex;
  align-items: center;
  height: 40px;
}
</style>

